<template>
  <div class="layout">
    <div class="breadcrumb">
      <!-- 面包屑 -->
      <el-breadcrumb separator=">">
        <el-breadcrumb-item :to="{ path: '/' }">首页</el-breadcrumb-item>
        <el-breadcrumb-item
          :to="{ path: `/category/${categoryFilterData.parentId}` }"
        >
          {{ categoryFilterData.parentName }}
        </el-breadcrumb-item>
        <el-breadcrumb-item>{{ categoryFilterData.name }}</el-breadcrumb-item>
      </el-breadcrumb>
    </div>
    <div class="tabs">
      <el-tabs
        type="border-card"
        v-model="reqData.sortFilter"
        @tab-change="tabChange"
      >
        <el-tab-pane label="最新商品" name="publishTime"></el-tab-pane>
        <el-tab-pane label="最高人气" name="orderNum"></el-tab-pane>
        <el-tab-pane label="评论最多" name="evaluateNum"></el-tab-pane>
      </el-tabs>
    </div>
    <!-- 商品列表 -->
    <div class="content">
      <div class="content_item" v-infinite-scroll="load" :infinite-scroll-disabled="disabled">
        <div v-for="good in goodsData" :key="good.id">
          <GoodItem :goods="good" />
        </div>
      </div>
    </div>
  </div>
</template>

<script setup>
import { getCategoryFilterAPI, getSubCategoryAPI } from "@/apis/category";
import { onMounted, ref } from "vue";
import { useRoute } from "vue-router";
import GoodItem from "@/views/Home/components/GoodItem.vue";

const categoryFilterData = ref({});
const route = useRoute();
const goodsData = ref([]);
const disabled = ref(false)
const reqData = ref({
  categoryId: route.params.id,
  page: 1,
  pageSize: 20,
  sortFilter: "publishTime",
});

//获取面包屑导航数据
const getCategoryFilter = async () => {
  const res = await getCategoryFilterAPI(route.params.id);
  categoryFilterData.value = res.result;
};

//获取基础列表数据
const getGoodsData = async () => {
  const res = await getSubCategoryAPI(reqData.value);
  goodsData.value = res.result.items;
};

onMounted(async () => {
  await getCategoryFilter();
  await getGoodsData();
});

// tab切换回调
const tabChange = () => {
  //初始化页数
  reqData.value.page = 1;
  getGoodsData();
};

//加载更多
const load = async () => {
  //获取下一页数据
  reqData.value.page ++
  const res = await getSubCategoryAPI(reqData.value)
  goodsData.value = [...goodsData.value,...res.result.items]

  //加载完毕,停止监听
  if(res.result.items.length === 0){
    disabled.value = true
  }
}

</script>

<style lang="scss" scoped>
.layout {
  .breadcrumb {
    padding: 20px 50px;
  }
  .tabs {
    padding: 0;
  }
  .content {
    background: white;
    padding: 20px 0;
    .content_item {
      display: grid;
      grid-template-columns: repeat(4, 1fr);
      gap: 10px;
      padding-bottom: 10px;
      padding-left: 30px;
    }
  }
}
</style>
